package com.ruoyi.SuveryInterface.IdentitySuvery;

/**
 * @Description:
 * @Author: 吕宇宏
 * @Date: 2024/7/1 16:41
 */
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.SuveryInterface.Imp.domain.*;
import com.ruoyi.SuveryInterface.Imp.mapper.*;
import com.ruoyi.SuveryInterface.NegativeSuvery.AES;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class IdentitySuveryImp {
    //接口地址
   private static String apiUrl = "https://qyss.cjbdi.com/qyss/qyss/fyss/identitySsSxXgCase";
    //加密密钥
    private static  String key = "QCzpcXY1eidkQmcq";
    //接口调用凭证
    private static  String ticket = "o0djGDRrMr9QN4xpknZ7C3wuNDcu3T+Ylz0XRByOl7k=";

    public IdentitySuveryImp(SysIdentityMapper identityMapper, SysIdentityCasesMapper identityCasesMapper, SysIdentityDsrxxMapper dsrxxMapper, SysIdentitySxbzxrMapper identitySxbzxrMapper, SysIdentityXgbzxrMapper xgbzxrMapper) {
        this.identityMapper = identityMapper;
        this.identityCasesMapper = identityCasesMapper;
        this.dsrxxMapper = dsrxxMapper;
        this.identitySxbzxrMapper = identitySxbzxrMapper;
        this.xgbzxrMapper = xgbzxrMapper;
    }

    public SysIdentityMapper getIdentityMapper() {
        return identityMapper;
    }

    public void setIdentityMapper(SysIdentityMapper identityMapper) {
        this.identityMapper = identityMapper;
    }

    public SysIdentityCasesMapper getIdentityCasesMapper() {
        return identityCasesMapper;
    }

    public void setIdentityCasesMapper(SysIdentityCasesMapper identityCasesMapper) {
        this.identityCasesMapper = identityCasesMapper;
    }

    public SysIdentityDsrxxMapper getDsrxxMapper() {
        return dsrxxMapper;
    }

    public void setDsrxxMapper(SysIdentityDsrxxMapper dsrxxMapper) {
        this.dsrxxMapper = dsrxxMapper;
    }

    public SysIdentitySxbzxrMapper getIdentitySxbzxrMapper() {
        return identitySxbzxrMapper;
    }

    public void setIdentitySxbzxrMapper(SysIdentitySxbzxrMapper identitySxbzxrMapper) {
        this.identitySxbzxrMapper = identitySxbzxrMapper;
    }

    public SysIdentityXgbzxrMapper getXgbzxrMapper() {
        return xgbzxrMapper;
    }

    public void setXgbzxrMapper(SysIdentityXgbzxrMapper xgbzxrMapper) {
        this.xgbzxrMapper = xgbzxrMapper;
    }

    private SysIdentityMapper identityMapper;

    private SysIdentityCasesMapper identityCasesMapper;

    private SysIdentityDsrxxMapper dsrxxMapper;

    private SysIdentitySxbzxrMapper identitySxbzxrMapper;

    private SysIdentityXgbzxrMapper xgbzxrMapper;

    public List<SysIdentity> QueryIndentity(String name,String id,Integer authorize,Long orderId) throws Exception {
        if (true) {
            throw new Exception();
        }


        //入参封装
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("name", name);
        jsonObject.put("id", id);
        jsonObject.put("authorize", authorize);
        jsonArray.add(jsonObject);

        int outTime = 60000;
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(outTime)
                .setConnectTimeout(outTime)
                .setConnectionRequestTimeout(outTime)
                .build();
        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultRequestConfig(defaultRequestConfig)
                .build();
        HttpPost httpPost = new HttpPost(apiUrl);
        //添加header
        httpPost.addHeader("Content-type", "application/json; charset=utf-8");
        httpPost.addHeader("ticket", ticket);

            //需要对入参进行aes加密
            String requestBody = AES.aesEncrypt(jsonArray.toJSONString(), key);
            //添加body
            ByteArrayEntity entity = new ByteArrayEntity(requestBody.getBytes("UTF-8"));
            entity.setContentType("application/json");
            httpPost.setEntity(entity);

            //执行post请求
            HttpResponse response = httpClient.execute(httpPost);
            String result = EntityUtils.toString(response.getEntity());
            //接收到返回结果需要进行aes解密
            String x = AES.aesDecrypt(result, key);

            List<SysIdentity> identityEntity = getIdentityEntity(x);

            insertSql(identityEntity,orderId);


            return identityEntity;


    }

    private void insertSql(List<SysIdentity> identityEntity,Long orderId) {
        //执行插入操作
        for (SysIdentity sysIdentity : identityEntity) {
            //1插入主表sys_identity

            sysIdentity.setOrderId(orderId);
            List<SysIdentity> sysIdentities = new ArrayList<>();
            sysIdentities.add(sysIdentity);
            identityMapper.insertBatch(sysIdentities);
            List<SysIdentityDsrxx> identityDsrxxes = new ArrayList<>();
            //2.如果存在dzmx则进行插入 type为0

            if (sysIdentity.getDzmx().size()>0) {
                List<JSONObject> dzmxs = sysIdentity.getDzmx();
                List<SysIdentityCases> identityCases = new ArrayList<>();
                for (JSONObject dzmx : dzmxs) {
                    dzmx.put("identityId",sysIdentity.getId());
                    dzmx.put("type",0);
                    SysIdentityCases sysIdentityCases = dzmx.toJavaObject(SysIdentityCases.class);
                    identityCases.add(sysIdentityCases);
                }


                identityCasesMapper.insertBatch(identityCases);

                //判断是否存在c_dsrxx
                for (SysIdentityCases identityCase : identityCases) {

                    if(identityCase.getCDsrxx()!=null) {
                        List<JSONObject> cDsrxx = identityCase.getCDsrxx();

                        for (JSONObject dsrxx : cDsrxx) {
                            //todo
                            dsrxx.put("casesId",identityCase.getId());
                            SysIdentityDsrxx sysIdentityDsrxx = dsrxx.toJavaObject(SysIdentityDsrxx.class);
                            identityDsrxxes.add(sysIdentityDsrxx);
                        }
                    }

                }


            }

            //如果civil不为空 插入  type为1  民事
            if(sysIdentity.getCivil()!=null) {
                List<JSONObject> civils = sysIdentity.getCivil();
                List<SysIdentityCases> identityCases = new ArrayList<>();
                for (JSONObject civil : civils) {
                    civil.put("identityId",sysIdentity.getId());
                    civil.put("type",1);
                    SysIdentityCases sysIdentityCases = civil.toJavaObject(SysIdentityCases.class);
                    identityCases.add(sysIdentityCases);


                }


                identityCasesMapper.insertBatch(identityCases);
                //判断是否为空

                for (SysIdentityCases identityCase : identityCases) {

                    if(identityCase.getCDsrxx()!=null) {
                        List<JSONObject> cDsrxx = identityCase.getCDsrxx();

                        for (JSONObject dsrxx : cDsrxx) {
                            //todo
                            dsrxx.put("casesId",identityCase.getId());
                            SysIdentityDsrxx sysIdentityDsrxx = dsrxx.toJavaObject(SysIdentityDsrxx.class);
                            identityDsrxxes.add(sysIdentityDsrxx);
                        }
                    }

                }

            }
            //如果implement不为空 插入  type为2  执行
            if(sysIdentity.getImplement()!=null) {
                List<JSONObject> implementes = sysIdentity.getImplement();
                List<SysIdentityCases> identityCases = new ArrayList<>();
                for (JSONObject implemente : implementes) {
                    implemente.put("identityId",sysIdentity.getId());
                    implemente.put("type",2);
                    SysIdentityCases sysIdentityCases = implemente.toJavaObject(SysIdentityCases.class);
                    identityCases.add(sysIdentityCases);
                }


                identityCasesMapper.insertBatch(identityCases);
                for (SysIdentityCases identityCase : identityCases) {

                    if(identityCase.getCDsrxx()!=null) {
                        List<JSONObject> cDsrxx = identityCase.getCDsrxx();

                        for (JSONObject dsrxx : cDsrxx) {
                            //todo
                            dsrxx.put("casesId",identityCase.getId());
                            SysIdentityDsrxx sysIdentityDsrxx = dsrxx.toJavaObject(SysIdentityDsrxx.class);
                            identityDsrxxes.add(sysIdentityDsrxx);
                        }
                    }

                }
            }
            if (identityDsrxxes.size()>0) {
                dsrxxMapper.insertBatch(identityDsrxxes);
            }


            //如果sxbzxr不为空 则插入
            if(sysIdentity.getSxbzxr().size()>0) {
                List<JSONObject> sxbzxrs = sysIdentity.getSxbzxr();

                List<SysIdentitySxbzxr> identitySxbzxrs = new ArrayList<>();
                for (JSONObject sxbzxr : sxbzxrs) {
                    sxbzxr.put("identityId",sysIdentity.getId());
                    sxbzxr.put("dataId",sxbzxr.get("id"));
                    sxbzxr.remove("id");

                    SysIdentitySxbzxr sysIdentitySxbzxr = sxbzxr.toJavaObject(SysIdentitySxbzxr.class);
                    identitySxbzxrs.add(sysIdentitySxbzxr);

                }


                identitySxbzxrMapper.insertBatch(identitySxbzxrs);
            }

            //如果xgbzxr不为空 则插入
            if(sysIdentity.getXgbzxr().size()>0) {
                List<JSONObject> xgbzxrs = sysIdentity.getXgbzxr();

                List<SysIdentityXgbzxr> identityXgbzxr = new ArrayList<>();
                for (JSONObject xgbzxr : xgbzxrs) {
                    xgbzxr.put("identityId",sysIdentity.getId());
                    xgbzxr.put("dataId",xgbzxr.get("id"));
                    xgbzxr.remove("id");
                    SysIdentityXgbzxr sysIdentityXgbzxr = xgbzxr.toJavaObject(SysIdentityXgbzxr.class);
                    identityXgbzxr.add(sysIdentityXgbzxr);

                }


                xgbzxrMapper.insertBatch(identityXgbzxr);
            }

        }
    }


    private List<SysIdentity> getIdentityEntity(String code) {
        SysIdentity sysIdentity = JSONObject.parseObject(code, SysIdentity.class);

        List<SysIdentity> sysIdentities = new ArrayList<>();
        //说明是多个数据
        List<JSONObject> jsonObjects = JSONObject.parseObject(sysIdentity.getData(),List.class);
        for (JSONObject jsonObject : jsonObjects) {
            SysIdentity sysIdentity01 = new SysIdentity();
            BeanUtils.copyProperties(sysIdentity,sysIdentity01);
            sysIdentity01.setCardId(String.valueOf(jsonObject.get("id")));
            sysIdentity01.setIdentity((String) jsonObject.get("identity"));

            if (jsonObject.get("dzmx")!=null) {
                List<JSONObject> dzmx = JSONObject.parseObject(jsonObject.get("dzmx").toString(),List.class);

                sysIdentity01.setDzmx(dzmx);
            }
            if (jsonObject.get("gkmx")!=null) {
                JSONObject gkmx = JSONObject.parseObject(jsonObject.get("gkmx").toString());
                sysIdentity01.setCrc(String.valueOf(gkmx.get("crc")));
                if (gkmx.get("civil")!=null) {
                    JSONObject civil = JSONObject.parseObject(String.valueOf(gkmx.get("civil")));
                    List<JSONObject> civilCases = JSONObject.parseObject(String.valueOf(civil.get("cases")), List.class);
                    sysIdentity01.setCivil(civilCases);
//                            for (JSONObject civilCase : civilCases) {
//                                if (civilCase.get("c_dsrxx")!=null) {
//                                    List<JSONObject> c_dsrxx = JSONObject.parseObject(civilCase.get("c_dsrxx").toString(), List.class);
//
//                                }
//                            }

                }
                if (gkmx.get("implement")!=null) {
                    JSONObject implement = JSONObject.parseObject(String.valueOf(gkmx.get("implement")));
                    List<JSONObject> implementCases = JSONObject.parseObject(String.valueOf(implement.get("cases")), List.class);

                    sysIdentity01.setImplement(implementCases);
                }




            }

            if (jsonObject.get("sxbzxr")!=null) {
                List<JSONObject> sxbzxr = JSONObject.parseObject(jsonObject.get("sxbzxr").toString(),List.class);

                sysIdentity01.setSxbzxr(sxbzxr);
            }
            if (jsonObject.get("xgbzxr")!=null) {
                List<JSONObject> xgbzxr = JSONObject.parseObject(jsonObject.get("xgbzxr").toString(), List.class);

                sysIdentity01.setXgbzxr(xgbzxr);
            }
            sysIdentity01.setCreateTime(new Date());

            //todo
            sysIdentity01.setCreateBy(1L);
            sysIdentities.add(sysIdentity01);

        }
        return sysIdentities;

    }



}
